package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void shoppingCartAdd(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝传入的参数
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //将线程空间中的id赋值进来
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //根据条件查询菜品，套餐
        List<ShoppingCart> list = shoppingCartMapper.getShoppingCart(shoppingCart);

        //判断菜品，套餐为不为空，不为空就只需要在数量加1
        if (list != null && list.size() > 0){
            shoppingCart= list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //为空就插入添加的菜品，套餐
            if (shoppingCartDTO.getDishId() != null){
                Dish byId = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
            } else {
                Setmeal status = setmealMapper.getStatus(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(status.getName());
                shoppingCart.setImage(status.getImage());
                shoppingCart.setAmount(status.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @param
     * @return
     */
    @Override
    public List<ShoppingCart> get() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart build = ShoppingCart.builder()
                .userId(currentId)
                .build();
        return shoppingCartMapper.getShoppingCart(build);
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart build = ShoppingCart.builder()
                .userId(currentId)
                .build();
        shoppingCartMapper.delete(build);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.getShoppingCart(shoppingCart);
            shoppingCart = list.get(0);

        if (shoppingCart.getNumber() > 1){
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            shoppingCartMapper.deleteById(shoppingCart);
        }
    }
}
